﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using Museum.DBAccess;

public partial class SchoolDetails : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            ListBox1.DataSource = DBAccess.GetMuseumByCity("");
            ListBox1.DataBind();

            ddlCountry.DataSource = DBAccess.GetAllCountries();
            ddlCountry.DataBind();

            ddlCountry.Items.FindByText("United States").Selected = true;
            BindControls();
        }

        // from edit
        if (Request.QueryString["SchoolID"] != null)
            BindSchoolData();
        else
        {
            if (ViewState["FirstTime"] == null)
            {
                ViewState["FirstTime"] = "1";
            }
            RefreshData();
        }
    }

    private void RefreshData()
    {
        // if data moved  
        if (ViewState["IsMovedToRight"] != null)
        {
            if (ViewState["IsMovedToRight"].ToString().Equals("1"))
            {
                // do nothing
            }
        }
        else
        {
            if (ViewState["FirstTime"] != null && ViewState["FirstTime"].ToString().Equals("1"))
            {
                ListBox1.DataSource = DBAccess.GetMuseumByCity("");
                ListBox1.DataBind();
                ClearControls();
                ViewState["FirstTime"] = "0";
            }
        }
    }
    private void ClearControls()
    {
        txtSchoolName.Text = "";
        txtSchoolAddress.Text = "";
        ListBox2.Items.Clear();
    }

    protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindControls();
    }

    private void BindControls()
    {
        DataTable dtCities = new DataTable();
        try
        {
            //load cities from DB
            dtCities = DBAccess.GetCities(Convert.ToInt32(ddlCountry.SelectedValue), DateTime.Parse(Constants.CityMinDate));
            if (dtCities != null)
            {
                ddlCity.DataSource = dtCities;
                ddlCity.DataBind();
            }
        }
        catch
        {
        }
        finally
        {
        }
    }

    protected void btnMoveRight_Click(object sender, System.EventArgs e)
    {
        for (int i = ListBox1.Items.Count - 1; i >= 0; i--)
        {
            if (ListBox1.Items[i].Selected == true)
            {
                ListBox2.Items.Add(ListBox1.Items[i]);
                ListItem li = ListBox1.Items[i];
                ListBox1.Items.Remove(li);
            }
        }
        ViewState["IsMovedToRight"] = "1";
    }

    protected void btnMoveLeft_Click(object sender, System.EventArgs e)
    {
        for (int i = ListBox2.Items.Count - 1; i >= 0; i--)
        {
            if (ListBox2.Items[i].Selected == true)
            {
                ListBox1.Items.Add(ListBox2.Items[i]);
                ListItem li = ListBox2.Items[i];
                ListBox2.Items.Remove(li);
            }
        }
    }

    /// <summary>
    /// Binds my category data.
    /// </summary>
    private void BindSchoolData()
    {
        DataTable dtSchool = new DataTable();
        int schoolID = 0;
        try
        {
            if (Request.QueryString["SchoolID"] != null)
                schoolID = Convert.ToInt32(Request.QueryString["SchoolID"]);

            dtSchool = DBAccess.GetSchoolDetails(schoolID);

            if (dtSchool != null && dtSchool.Rows.Count > 0)
            {
                ddlCountry.SelectedValue = dtSchool.Rows[0]["CountryId"].ToString();
                ddlCity.DataSource = DBAccess.GetCities(Convert.ToInt32(ddlCountry.SelectedValue), Convert.ToDateTime(Constants.CityMinDate));
                ddlCity.DataBind();
                ddlCity.SelectedValue = dtSchool.Rows[0]["CityId"].ToString();
                txtSchoolName.Text = dtSchool.Rows[0]["SchoolName"].ToString();
                txtSchoolAddress.Text = dtSchool.Rows[0]["Address"].ToString();
                for (int j = 0; j < dtSchool.Rows.Count; j++)
                {
                    for (int i = ListBox1.Items.Count - 1; i >= 0; i--)
                    {
                        if (ListBox1.Items[i].Value.Equals(dtSchool.Rows[j]["MuseumId"].ToString()))
                        {
                            ListBox2.Items.Add(ListBox1.Items[i]);
                            ListItem li = ListBox1.Items[i];
                            ListBox1.Items.Remove(li);
                        }
                    }
                }
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
        }
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        int schoolId = 0;
        string museumIds = string.Empty;
        try
        {
            if (Request.QueryString["SchoolID"] != null)
                schoolId = Convert.ToInt32(Request.QueryString["SchoolID"]);

            for (int i = 0; i < ListBox2.Items.Count; i++)
            {
                museumIds += ListBox2.Items[i].Value + ",";
            }
            if (museumIds.Length > 0)
                museumIds = museumIds.Substring(0, museumIds.Length - 1);

            DBAccess.InsertOrUpdateSchoolDetails(schoolId, museumIds, txtSchoolName.Text.Trim(), txtSchoolAddress.Text.Trim(), Convert.ToInt32(ddlCity.SelectedValue));

            if (schoolId < 1)
                Alert.ShowMessage("School created successfully.");
            else
                Alert.ShowMessage("School updated successfully.");

            Response.Redirect("ManageSchool.aspx", false);
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    protected void btnReset_Click(object sender, EventArgs e)
    {
        txtSchoolName.Text = "";
        txtSchoolAddress.Text = "";
    }
    //protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
    //{
    //    ViewState["ListSelectIndex"] = ListBox1.SelectedIndex;
    //    RefreshData();
    //}
}
